<?php

namespace Home\Controller;

class PhotoController extends BaseController {

    public function _initialize() {
        $userToken = '';
        if (I("user_token")) {
            $userToken = I("user_token");
        }
        //查询库
        $where['user_token'] = $userToken;
        $userTokenInfo = M('Users')->where($where)->find();
        if ($userTokenInfo) {
            $this->user_id = $userTokenInfo['user_id'];
        }
    }

    public function getList() {
        $p = I('p', 0, 'intval');
        $size = I('psize', 15, 'intval');
        if ($p) {
            $page = $p * $size;
        }

        $dog_id = I('dog_id', 0, 'intval');

        if ($dog_id == 0) {
            $this->error('请选择正确的狗卡');
        }

        $dog_info = M('Dog')->field('dog_id,user_id,dog_name,dog_face,describe')->where(['dog_id' => $dog_id, 'is_del' => 0])->find();

        if (empty($dog_info)) {
            $this->error('没有找到写真数据');
        }

        $dog_info['dog_face'] = 'https://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $dog_info['dog_face'];
        $dog_info['is_edit'] = 0;
        if ($this->user_id == $dog_info['user_id']) {
            $dog_info['is_edit'] = 1;
        }

        unset($dog_info['user_id']);

        $list = M()
                ->table('ot_photo p')
                ->field('p.photo_id,p.photo_url,p.like_num')
                ->join('ot_dog d on p.dog_id = d.dog_id')
                ->join('ot_users u on d.user_id = u.user_id')
                ->where(['p.dog_id' => $dog_id, 'p.is_del' => 0, 'd.is_del' => 0])
                ->limit($page, $size)
                ->order('p.create_time desc')
                ->select();

        if (!empty($list)) {
            foreach ($list as &$val) {
                $val['photo_url'] = 'https://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $val['photo_url'];
                $val['is_like'] = 0;

                $is_like = M('PhotoLike')->where(['photo_id' => $val['photo_id'], 'user_id' => $this->user_id])->find();

                if ($is_like) {
                    $val['is_like'] = 1;
                }
            }
            unset($val);
        }else{
            $list = [];
        }

        $detail['dog_info'] = $dog_info;
        $detail['photo_num'] = M('Photo')->where(['dog_id' => $dog_id, 'is_del' => 0])->count();
        $detail['photo_list'] = $list;

        $this->success('获取成功', $detail);
    }

    public function likeOrCancel() {

        if ($this->user_id == 0) {
            $this->error('请登录', '', 101);
        }

        $id = I('id', 0, 'intval');

        $photo = M('Photo')->where(['photo_id' => $id, 'is_del' => 0])->find();

        if (empty($photo)) {
            $this->error('请选择正确的写真');
        }

        $is_like = M('PhotoLike')->where(['photo_id' => $id, 'user_id' => $this->user_id])->find();

        $res = 0;
        if ($is_like) {
            $res = M('PhotoLike')->where(['like_id' => $is_like['like_id']])->delete();

            M('Photo')->where(['photo_id' => $id])->setDec('like_num');
        } else {
            $data['photo_id'] = $id;
            $data['user_id'] = $this->user_id;

            $res = M('PhotoLike')->add($data);
            M('Photo')->where(['photo_id' => $id])->setInc('like_num');
        }

        if ($res) {
            $this->success();
        } else {
            $this->error();
        }
    }

    public function add() {
        if ($this->user_id == 0) {
            $this->error('请登录', '', 101);
        }

        $dog_id = I('dog_id', 0, 'intval');

        if ($dog_id == 0) {
            $this->error('请选择正确的狗卡');
        }

        $is_my_dog = M('Dog')->where(['dog_id' => $dog_id, 'user_id' => $this->user_id])->find();

        if (empty($is_my_dog)) {
            $this->error('不是你的狗');
        }

        $photo_urls = I('photo_url');

        if (empty($photo_urls)) {
            $this->error('请上传写真');
        }

        $photo_url = explode(',', $photo_urls);

        if (count($photo_url) > 9) {
            $this->error('一次最多上传9张图片');
        }
        $data['dog_id'] = $dog_id;
        $data['group_id'] = 0;
        foreach ($photo_url as $val) {
            $data['photo_url'] = $val;

            $res = M('Photo')->add($data);

            if ($data['group_id'] == 0) {
                $data['group_id'] = $res;
                M('Photo')->where(['photo_id' => $res])->save($data);

                $track['relation_id'] = $res;
                $track['dog_id'] = $dog_id;
                $track['type'] = 2;

                M('Track')->add($track);
            }
        }

        if ($res) {
            $this->success('发布成功');
        } else {
            $this->error('发布失败');
        }
    }

    public function del() {
        if ($this->user_id == 0) {
            $this->error('请登录', '', 101);
        }

        $photo_id = I('id');

        if ($photo_id == 0) {
            $this->error('请选择正确的写真');
        }

        $dog_ids = M('Dog')->where(['user_id' => $this->user_id])->getField('dog_id', true);

        $data['is_del'] = 1;

        $where['photo_id'] = array('in', explode(',', $photo_id));

        $where['dog_id'] = array('in', $dog_ids);
        $res = M('Photo')->where($where)->save($data);

        if ($res) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function getGroup() {
        $photo_id = I('id', 0, 'intval');

        if ($photo_id == 0) {
            $this->error('请选择正确的写真');
        }

        $photo_info = M('Photo')->where(['photo_id' => $photo_id, 'is_del' => 0])->find();

        if (empty($photo_info)) {
            $this->error('没有找到相关写真集');
        }

        $is_edit = 0;

        $is_my_dog = M('Dog')->where(['dog_id' => $photo_info['dog_id'], 'is_del' => 0])->find();

        if ($is_my_dog['user_id'] == $this->user_id) {
            $is_edit = 1;
        }

        $photo_group = M('Photo')->field('photo_id,photo_url,like_num,group_id')->where(['group_id' => $photo_info['group_id'], 'is_del' => 0])->select();

        if (!empty($photo_group)) {
            foreach ($photo_group as &$val) {
                $val['photo_url'] = 'https://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $val['photo_url'];
                $val['is_like'] = 0;

                $is_like = M('PhotoLike')->where(['photo_id' => $val['photo_id'], 'user_id' => $this->user_id])->find();

                if ($is_like) {
                    $val['is_like'] = 1;
                }

                $val['is_edit'] = $is_edit;
            }
            unset($val);
        }

        $this->success('获取成功', $photo_group);
    }

    public function detail() {
        $photo_id = I('id', 0, 'intval');

        if ($photo_id == 0) {
            $this->error('请选择正确的写真');
        }

        $photo_info = M('Photo')->field('photo_id,photo_url,like_num')->where(['photo_id' => $photo_id, 'is_del' => 0])->find();

        if (empty($photo_info)) {
            $this->error('没有找到相关写真集');
        }

        $is_edit = 0;

        $is_my_dog = M('Dog')->where(['dog_id' => $photo_info['dog_id'], 'is_del' => 0])->find();

        if ($is_my_dog['user_id'] == $this->user_id) {
            $is_edit = 1;
        }


        $photo_info['photo_url'] = 'https://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $photo_info['photo_url'];
        $photo_info['is_like'] = 0;

        $is_like = M('PhotoLike')->where(['photo_id' => $photo_info['photo_id'], 'user_id' => $this->user_id])->find();

        if ($is_like) {
            $photo_info['is_like'] = 1;
        }

        $photo_info['is_edit'] = $is_edit;

        $this->success('获取成功', $photo_info);
    }

}
